package br.com.treinamento.gestaoExperienciaProfissional.util;

/**
 * @author cdcarvalho
 *
 */
public final class ValidadorUtil {

	private ValidadorUtil() {
	}

	public static boolean validarCPF(String value) {

		if (value.equals("00000000000") || value.equals("11111111111")
				|| value.equals("22222222222") || value.equals("33333333333")
				|| value.equals("44444444444") || value.equals("55555555555")
				|| value.equals("66666666666") || value.equals("77777777777")
				|| value.equals("88888888888") || value.equals("99999999999")
				|| (value.length() != 11)) {
			return false;
		} else {
			return calcDigVerif(value.substring(0, 9)).equals(
					value.substring(9, 11));
		}

	}

	public static String calcDigVerif(String value) {
		Integer primDig, segDig;
		int soma = 0, peso = 10;
		for (int i = 0; i < value.length(); i++)
			soma += Integer.parseInt(value.substring(i, i + 1)) * peso--;

		if (soma % 11 == 0 | soma % 11 == 1)
			primDig = new Integer(0);
		else
			primDig = new Integer(11 - (soma % 11));

		soma = 0;
		peso = 11;
		for (int i = 0; i < value.length(); i++)
			soma += Integer.parseInt(value.substring(i, i + 1)) * peso--;

		soma += primDig.intValue() * 2;
		if (soma % 11 == 0 | soma % 11 == 1)
			segDig = new Integer(0);
		else
			segDig = new Integer(11 - (soma % 11));

		return primDig.toString() + segDig.toString();
	}

	public static String cpfSemFormatacao(String value){
		return value.replace(".","").replace("-", "");
	}
	
}
